# -*- coding: utf-8 -*-
import wsgiref.handlers

from google.appengine.ext import webapp
from google.appengine.ext.webapp import template

from BeautifulSoup import BeautifulSoup

from xmw import tianya

from xmw.tianya import error_tianya
import os
import urllib2
import uuid

import datetime

class MainHandler(webapp.RequestHandler):
    def get(self):
        if  self.request.GET.has_key('url'):
            url=self.request.GET['url']
            page = urllib2.urlopen(url)
            soup = BeautifulSoup(page)
        
            page.close()
            
            #get thread info

            #thread_title=soup.find('td',id='title')
            #created=soup.find('td',id='created')
            #mail_box=soup.find('td',id='mail_box')
            thread_status=soup.find('td',id='thread_status').string
        
            #get page info
            #pages   =soup.find('table',id='pages')
            #page_seqs   =soup.findAll('td',{'class':'page_seq'})
            page_urls=soup.findAll('td',{'class':'page_url'})
            #page_ids    =   soup.findAll('td',{'class':'page_id'})
            page_latest_post_update_dates    =   soup.findAll('td',{'class':'page_latest_post_update_date'})
            page_statuss    =   soup.findAll('td',{'class':'page_status'})
            page_last_scans =     soup.findAll('td',{'class':'page_last_scan'})
        
        
            #thread=tianya.Thread(title=str(thread_title),thread_id=str(thread_id),status=str(thread_status))
            """                    
            self.response.out.write("<br>thread_id:%s"%str(thread_id))        
            self.response.out.write("<br>thread_title:%s"%str(thread_title))
            #self.response.out.write("<br>created:%s"%str(created))
            self.response.out.write("<br>mail_box:%s"%str(mail_box))
            self.response.out.write("<br>thread_status:%s"%str(thread_status))
            self.response.out.write("<br>pages:%s"%str(pages))
            self.response.out.write("<br>page_seqs:%s"%str(page_seqs))
            self.response.out.write("<br>page_urls:%s"%str(page_urls))
            self.response.out.write("<br>page_ids:%s"%str(page_ids))
            self.response.out.write("<br>page_latest_post_update_dates:%s"%str(page_latest_post_update_dates))
            self.response.out.write("<br>page_statuss:%s"%str(page_statuss))
            self.response.out.write("<br>page_last_scans:%s"%str(page_last_scans))
            self.response.out.write("<br>page_last_scans:%s"%str(page_last_scans[0]))
            """
            lst_page=[]
            lst_page_url=[]
            for i in range(len(page_urls)):
                latest_post_date_str=str(page_latest_post_update_dates[i].string)
                latest_post_date = datetime.datetime.strptime(latest_post_date_str,"%Y-%m-%d %H:%M:%S")
                
                last_scan_str=str(page_last_scans[i].string)
                last_scan_date=datetime.datetime.strptime(last_scan_str,"%Y-%m-%d %H:%M:%S")
                #date=datetime.datetime(date) 
                page_id=uuid.uuid1().hex
                page=tianya.Page(page_id=str(page_id),\
                                #seq_in_thread =int(page_seqs[i].string),\
                                seq_in_thread =i,\
                                #int(str(page_seqs[i])),\
                                url =   str(page_urls[i].string),\
                                latest_post_date=latest_post_date,\
                                page_status = str(page_statuss[i]),\
                                last_scan = last_scan_date
                                #thread_id   =   str(thread_id)
                                )
                lst_page.append(page)
                lst_page_url.append(page.url)
                
     
            t=u"\u300a\u5c11\u6797\u5bfa\u4e0d\u5e94\u6218 \u5ce8\u7709\u6d3e\u8c6a\u8a00\u6a2a\u626b\u6cf0\u62f3\u738b\u300b\u4e00\u6587\u8bb0\u8005\u63ed\u65b0\u95fb\u4e8b\u4ef6\u5185\u5e55_\u5929\u6daf\u6742\u8c08_\u5929\u6daf\u793e\u533a"
            t=u"大漠"
            #thread=tianya.Thread(title=str(thread_title).decode("gb18030").encode("utf-8"),thread_id=str(thread_id),status=str(thread_status),page_url=lst_page_url)
            thread_id=uuid.uuid1().hex
            #thread.thread_id=thread_id 
            thread=tianya.Thread(title=t,thread_id=str(thread_id),status=str(thread_status),page_url=lst_page_url)
            
            self.response.out.write("<BR>%s<BR>"%tianya.add_thread(thread))
            self.response.out.write("<BR>%s<BR>"%tianya.add_page(lst_page))
            
            #self.response.out.write("thread:%s"%str(thread.page_url))
        else:
            e = error_tianya.ARGUMENT_FAULT("URI: notify_new_thread needs parameter url.    /notify_new_thread?url=http://XXXX")
            e.log()
            template_values = {
                           "error":e
                           }     
            path = os.path.join(os.path.dirname(__file__), 'template/error.html')
            #self.response.out.write(path)
            self.response.out.write(template.render(path, template_values))


def main():
    import global_setting
    application = webapp.WSGIApplication([('/notify_new_thread', MainHandler)],
                                       debug=global_setting.is_debug())
    wsgiref.handlers.CGIHandler().run(application)


if __name__ == '__main__':
    main()
